Rather than creating a boxed primitive from a String
to extract the primitive value, use the relevant parse
method
instead. Using parse
makes the code more efficient and the intent of the developer clearer.
Noncompliant code example
String myNum = "42.0";
float myFloat = new Float(myNum); // Noncompliant
float myFloatValue = (new Float(myNum)).floatValue(); // Noncompliant
int myInteger = Integer.valueOf(myNum); // Noncompliant
int myIntegerValue = Integer.valueOf(myNum).intValue(); // Noncompliant
Compliant solution
String myNum = "42.0";
float f = Float.parseFloat(myNum);
int myInteger = Integer.parseInt(myNum);